package com.nineclock.system.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.nineclock.system.service.SysCompanyUserService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;


@Slf4j
    public class ExcelMemberListener extends AnalysisEventListener<ExcelMember> {

    private SysCompanyUserService companyUserService;

    private List<ExcelMember> memberList = new ArrayList<>();

    //ExcelMemberListener不能被spring管理，要每次读取excel都要new,然后里面用到bean, 可以构造方法传进去

    public ExcelMemberListener(){
    }

    public ExcelMemberListener(SysCompanyUserService sysCompanyUserService){

        this.companyUserService = sysCompanyUserService;
    }

    //解析每一行时触发
    @Override
    public void invoke(ExcelMember data, AnalysisContext context) {
        log.info("解析每一行的数据 , 数据内容: " + data);
        memberList.add(data);
    }

    //完全解析完毕后触发
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

        log.info("解析数据完毕");
        companyUserService.parsedData(memberList);
    }

}